<?php
abstract class Controller {
	protected $registry;	
	protected $id;
	protected $layout;
	protected $template;
	protected $children = array();
	protected $data = array();
	protected $output;
	
	public function __construct($registry) {
		$this->registry = $registry;
	}
	
	public function __get($key) {
		return $this->registry->get($key);
	}
	
	public function __set($key, $value) {
		$this->registry->set($key, $value);
	}
			
	protected function forward($route, $args = array()) {
		return new Action($route, $args);
	}

	protected function redirect($url, $status = 302) {
		header('Status: ' . $status);
		header('Location: ' . str_replace(array('&amp;', "\n", "\r"), array('&', '', ''), $url));
		exit();				
	}
	
	protected function getChild($child, $args = array()) {
		$action = new Action($child, $args);
	
		global $vqmod;
		if (file_exists($vqmod->modCheck($action->getFile()))) {
			require_once($vqmod->modCheck($action->getFile()));

			$class = $action->getClass();

			$controller = new $class($this->registry);

			$controller->{$action->getMethod()}($action->getArgs());
			
			return $controller->output;
		} else {
			trigger_error('Error: Could not load controller ' . $child . '!');
			exit();					
		}		
	}
	
	protected function render() {
		foreach ($this->children as $child) {
			$this->data[basename($child)] = $this->getChild($child);
		}

        #swith theme device
        if (!strpos($_SERVER['PHP_SELF'], 'admin')) {
            global $DeviceDetect, $_GET;
            $DeviceTheme = 'true';
            $UseMobile = $this->config->get('config_mobile_enabled');
            $UseTablet = $this->config->get('config_tablet_enabled');
            if ((isset($_GET['DeviceTheme']) && $_GET['DeviceTheme'] == 'false') || (isset($_SESSION['DeviceTheme']) && $_SESSION['DeviceTheme'] == 'false' && !isset($_GET['DeviceTheme']))) {
                $_SESSION['DeviceTheme'] = 'false';
                $DeviceTheme = 'false';
            } elseif ((isset($_GET['DeviceTheme']) && $_GET['DeviceTheme'] == 'true') || (!isset($_GET['DeviceTheme']))) {
                $_SESSION['DeviceTheme'] = 'true';
                $DeviceTheme = 'true';
            }
            if ($DeviceTheme == 'true') {
                if ($UseMobile && ($DeviceDetect->isIphone || $DeviceDetect->isAndroidPhone || $DeviceDetect->isTierIphone || $DeviceDetect->isTierRichCss || $DeviceDetect->isTierGenericMobile)) {
                    $nameTheme = substr($this->template, 0, strpos($this->template, '/'));
                    $mobileTemplate = strtr($this->template, array($nameTheme => 'mobile'));
                    #if (strpos($mobileTemplate, 'module')) {
                        if (file_exists(DIR_TEMPLATE . $mobileTemplate)) {
                            $this->template = $mobileTemplate;
                        }
                    #} else {
                    #    $this->template = $mobileTemplate;
                    #}
                } elseif ($UseTablet && $DeviceDetect->isTierTablet) {
                    $nameTheme = substr($this->template, 0, strpos($this->template, '/'));
                    $tabletTemplate = strtr($this->template, array($nameTheme => 'tablet'));
                    #if (strpos($tabletTemplate, 'module')) {
                        if (file_exists(DIR_TEMPLATE . $tabletTemplate)) {
                            $this->template = $tabletTemplate;
                        }
                    #} else {
                    #   $this->template = $tabletTemplate;
                    #}
                }
            }
        }
        
            
		
		
		global $vqmod;
		$file = $vqmod->modCheck(DIR_TEMPLATE . $this->template);
		if (file_exists($file)) {
		
			extract($this->data);
			
      		ob_start();
      
	  		require($file);
      
	  		$this->output = ob_get_contents();

      		ob_end_clean();
      		
			return $this->output;
    	} else {
			trigger_error('Error: Could not load template ' . DIR_TEMPLATE . $this->template . '!');
			exit();				
    	}
	}
}
?>